<script setup lang="ts">

import {RoomBedVO} from "@/api/ecg/doctor";
import {RoomApi} from "@/api/ecg/room";
import {ScreenQueueVO, ScreenApi} from "@/api/ecg/screen";
import {CallApi, CallVO} from "@/api/ecg/call";
import {queueStatusConvert} from "../../../utils/statusFormatter";
import {useCheckTypeStore} from "@/store/modules/checkType";

defineOptions({ name: 'roomscreen' })

const checkTypeStore = useCheckTypeStore()

const list = ref<ScreenQueueVO[]>([]) // 列表的数据
//const listPassed = ref<ScreenQueueVO[]>([]) // 过号列表的数据

let curSpeakPat : CallVO | null = null;

const roomBed = ref<RoomBedVO>({
  roomId : null,
  roomName: null,
  bedNo: null
})

const getList = async () => {
  const data = await ScreenApi.getRoomScreenData()
  list.value = data[1]
  // listPassed.value = data[2]
}

const getRoomByIp = async () => {
  const data = await RoomApi.getRoomByIP()
  roomBed.value = data
}

const startScrolling = () => {
  setInterval(() => {
    getList()
    if ( curSpeakPat === null ) {
      initiateSpeak()
    }
  }, 3000); // 每两秒滚动一次
}

const nameDesensitize = (patName) => {
  if (patName.length == 2) {
    //截取name 字符串截取第一个字符，
    return patName.substring(0, 1) + '*';
  } else if (patName.length == 3) {
    //截取第一个和第三个字符
    return patName.substring(0, 1) + '*' + patName.substring(2, 3);
  } else if (patName.length > 3) {
    //截取第一个和大于第4个字符
    return (
        patName.substring(0, 1) + '*' + '*' + patName.substring(3, patName.length)
    );
  }
}

const tableRowClassName = ({row, rowIndex}: {
  row: ScreenQueueVO
  rowIndex: number
}) => {
  if (row.status === 5) {
    return 'warning-row'
  } else if (row.status === 7) {
    return 'warning-row'  //return 'success-row'
  }
  return ''
}

const onSpeachEndEvent = async (event) => {
  console.log("Speech ended... " + event.currentTarget.text);
  curSpeakPat!.called = 1
  await CallApi.updateCall(curSpeakPat!)
  initiateSpeak()
}

const initiateSpeak = async () => {
  curSpeakPat = await CallApi.getNextInstallCall()
  if (curSpeakPat !== null) {
    speak("请、" + curSpeakPat.patName + "到" + curSpeakPat.roomName + "装机");
  }
}

const speak = (msg) => {
  console.info("speak " + msg);
  var speech = new SpeechSynthesisUtterance()
  speech.text = msg + "。。。" + msg + "。。。" + msg  + "。。。"
  speech.pitch = 1 // 获取并设置话语的音调(0-2 默认1，值越大越尖锐,越低越低沉)
  speech.rate = 0.9 // 获取并设置说话的速度(0.1-10 默认1，值越大语速越快,越小语速越慢)
  speech.volume = 100 // 获取并设置说话的音量
  speech.lang = 'zh-CN' // 设置播放语言

  speech.onend = onSpeachEndEvent
  speechSynthesis.speak(speech)
}

onMounted( () => {
  getRoomByIp()
  startScrolling()
})

</script>

<template>
  <el-container style="height: 100%;">
    <el-header style="font-size: 25px">{{ roomBed.roomName }}</el-header>
    <el-main>
      <el-table
          :data="list"
          :show-header="false"
          style="width: 100%; height: 100%; border: solid var(--el-color-primary-light-7); font-size: 18px;"
          :row-class-name="tableRowClassName"
      >
        <el-table-column
            prop="patName"
            label="预约序号"
            width="60px">
          <template #default="scope">
            <span style="display:inline-block; width:60px;">{{checkTypeStore.getCheckTypeSeqPrefix(scope.row.bookCheckType)}}{{scope.row.bookSeqNum}} </span>
          </template>
        </el-table-column>
        <el-table-column
            prop="patName"
            label="患者姓名"
            width="80px">
          <template #default="scope">
            <span style="display:inline-block; width:80px;">{{ nameDesensitize(scope.row.patName) }} </span>
          </template>
        </el-table-column>
        <el-table-column
            prop="patName"
            label="检查项目"
            width="80px">
          <template #default="scope">
            <span style="display:inline-block; width:80px;">{{scope.row.bookCheckType && checkTypeStore.getCheckTypeName(scope.row.bookCheckType)}} </span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" width="100px">
          <template #default="scope">
<!--            <dict-tag :type="DICT_TYPE.ECG_QUEUE_STATUS" :value="scope.row.status" />-->
            {{queueStatusConvert(scope.row.status)}}
          </template>
        </el-table-column>
      </el-table>
    </el-main>
    <el-button @click="speak('欢迎使用')" >欢迎使用</el-button>
<!--
    <el-footer height="100px" style="padding: 0 0">
          <el-header height="30px" style="background-color: #98b8e5; line-height: 30px;">过号区</el-header>
          <span v-for="(passedItem, index) in listPassed" :key="index">
                {{nameDesensitize(passedItem.patName) + "&nbsp;&nbsp;&nbsp;&nbsp;"}}
          </span>
    </el-footer>
-->
  </el-container>
</template>

<style scoped lang="scss">
.el-header, .el-footer {
  background-color: var(--el-color-primary-light-7);
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: var(--el-color-primary-light-7);
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: var(--el-color-primary-light-7);
  color: #333;
  padding: 0 0;
  text-align: center;
  line-height: 160px;
}

.el-table ::v-deep .warning-row {
  --el-table-tr-bg-color: var(--el-color-warning-light-9);
}
.el-table ::v-deep .success-row {
  --el-table-tr-bg-color: var(--el-color-success-light-9);
}

</style>
